home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2002 November / CD 1 / APC0211D1.ISO / workshop / prog / files / ActivePerl-5.6.1.633-MSWin32.msi / _dfd28670b580efd3528aaee6efd903c4 < prev    next >
Encoding:
Text File  |  2002-05-01  |  6.8 KB  |  245 lines

  1. #
  2. # $Id: Status.pm,v 1.26 1999/11/22 10:43:24 gisle Exp $
  3.  
  4. package HTTP::Status;
  5.  
  6. use strict;
  7. require 5.002;   # becase we use prototypes
  8.  
  9. =head1 NAME
  10.  
  11. HTTP::Status - HTTP Status code processing
  12.  
  13. =head1 SYNOPSIS
  14.  
  15.  use HTTP::Status;
  16.  
  17.  if ($rc != RC_OK) {
  18.      print status_message($rc), "\n";
  19.  }
  20.  
  21.  if (is_success($rc)) { ... }
  22.  if (is_error($rc)) { ... }
  23.  if (is_redirect($rc)) { ... }
  24.  
  25. =head1 DESCRIPTION
  26.  
  27. I<HTTP::Status> is a library of routines for defining and
  28. classifying HTTP status codes for libwww-perl.  Status codes are
  29. used to encode the overall outcome of a HTTP response message.  Codes
  30. correspond to those defined in RFC 2616 and RFC 2518.
  31.  
  32. =head1 CONSTANTS
  33.  
  34. The following constant functions can be used as mnemonic status code
  35. names:
  36.  
  37.    RC_CONTINUE                (100)
  38.    RC_SWITCHING_PROTOCOLS        (101)
  39.    RC_PROCESSING                        (102)
  40.  
  41.    RC_OK                (200)
  42.    RC_CREATED                (201)
  43.    RC_ACCEPTED                (202)
  44.    RC_NON_AUTHORITATIVE_INFORMATION    (203)
  45.    RC_NO_CONTENT            (204)
  46.    RC_RESET_CONTENT            (205)
  47.    RC_PARTIAL_CONTENT            (206)
  48.    RC_MULTI_STATUS                      (207)
  49.  
  50.    RC_MULTIPLE_CHOICES            (300)
  51.    RC_MOVED_PERMANENTLY            (301)
  52.    RC_FOUND                (302)
  53.    RC_SEE_OTHER                (303)
  54.    RC_NOT_MODIFIED            (304)
  55.    RC_USE_PROXY                (305)
  56.    RC_TEMPORARY_REDIRECT        (307)
  57.  
  58.    RC_BAD_REQUEST            (400)
  59.    RC_UNAUTHORIZED            (401)
  60.    RC_PAYMENT_REQUIRED            (402)
  61.    RC_FORBIDDEN                (403)
  62.    RC_NOT_FOUND                (404)
  63.    RC_METHOD_NOT_ALLOWED        (405)
  64.    RC_NOT_ACCEPTABLE            (406)
  65.    RC_PROXY_AUTHENTICATION_REQUIRED    (407)
  66.    RC_REQUEST_TIMEOUT            (408)
  67.    RC_CONFLICT                (409)
  68.    RC_GONE                (410)
  69.    RC_LENGTH_REQUIRED            (411)
  70.    RC_PRECONDITION_FAILED        (412)
  71.    RC_REQUEST_ENTITY_TOO_LARGE        (413)
  72.    RC_REQUEST_URI_TOO_LARGE        (414)
  73.    RC_UNSUPPORTED_MEDIA_TYPE        (415)
  74.    RC_REQUEST_RANGE_NOT_SATISFIABLE     (416)
  75.    RC_EXPECTATION_FAILED        (417)
  76.    RC_UNPROCESSABLE_ENTITY              (422)
  77.    RC_LOCKED                            (423)
  78.    RC_FAILED_DEPENDENCY                 (424)
  79.  
  80.    RC_INTERNAL_SERVER_ERROR        (500)
  81.    RC_NOT_IMPLEMENTED            (501)
  82.    RC_BAD_GATEWAY            (502)
  83.    RC_SERVICE_UNAVAILABLE        (503)
  84.    RC_GATEWAY_TIMEOUT            (504)
  85.    RC_HTTP_VERSION_NOT_SUPPORTED    (505)
  86.    RC_INSUFFICIENT_STORAGE              (507)
  87.  
  88. =cut
  89.  
  90. #####################################################################
  91.  
  92. use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
  93.  
  94. require Exporter;
  95. @ISA = qw(Exporter);
  96. @EXPORT = qw(is_info is_success is_redirect is_error status_message);
  97. @EXPORT_OK = qw(is_client_error is_server_error);
  98. $VERSION = sprintf("%d.%02d", q$Revision: 1.26 $ =~ /(\d+)\.(\d+)/);
  99.  
  100. # Note also addition of mnemonics to @EXPORT below
  101.  
  102. my %StatusCode = (
  103.     100 => 'Continue',
  104.     101 => 'Switching Protocols',
  105.     102 => 'Processing',                      # WebDAV
  106.     200 => 'OK',
  107.     201 => 'Created',
  108.     202 => 'Accepted',
  109.     203 => 'Non-Authoritative Information',
  110.     204 => 'No Content',
  111.     205 => 'Reset Content',
  112.     206 => 'Partial Content',
  113.     207 => 'Multi-Status',                    # WebDAV
  114.     300 => 'Multiple Choices',
  115.     301 => 'Moved Permanently',
  116.     302 => 'Found',
  117.     303 => 'See Other',
  118.     304 => 'Not Modified',
  119.     305 => 'Use Proxy',
  120.     307 => 'Temporary Redirect',
  121.     400 => 'Bad Request',
  122.     401 => 'Unauthorized',
  123.     402 => 'Payment Required',
  124.     403 => 'Forbidden',
  125.     404 => 'Not Found',
  126.     405 => 'Method Not Allowed',
  127.     406 => 'Not Acceptable',
  128.     407 => 'Proxy Authentication Required',
  129.     408 => 'Request Timeout',
  130.     409 => 'Conflict',
  131.     410 => 'Gone',
  132.     411 => 'Length Required',
  133.     412 => 'Precondition Failed',
  134.     413 => 'Request Entity Too Large',
  135.     414 => 'Request-URI Too Large',
  136.     415 => 'Unsupported Media Type',
  137.     416 => 'Request Range Not Satisfiable',
  138.     417 => 'Expectation Failed',
  139.     422 => 'Unprocessable Entity',            # WebDAV
  140.     423 => 'Locked',                          # WebDAV
  141.     424 => 'Failed Dependency',               # WebDAV
  142.     500 => 'Internal Server Error',
  143.     501 => 'Not Implemented',
  144.     502 => 'Bad Gateway',
  145.     503 => 'Service Unavailable',
  146.     504 => 'Gateway Timeout',
  147.     505 => 'HTTP Version Not Supported',
  148.     507 => 'Insufficient Storage',            # WebDAV
  149. );
  150.  
  151. my $mnemonicCode = '';
  152. my ($code, $message);
  153. while (($code, $message) = each %StatusCode) {
  154.     # create mnemonic subroutines
  155.     $message =~ tr/a-z \-/A-Z__/;
  156.     $mnemonicCode .= "sub RC_$message () { $code }\t";
  157.     # make them exportable
  158.     $mnemonicCode .= "push(\@EXPORT, 'RC_$message');\n";
  159. }
  160. # warn $mnemonicCode; # for development
  161. eval $mnemonicCode; # only one eval for speed
  162. die if $@;
  163.  
  164. # backwards compatibility
  165. *RC_MOVED_TEMPORARILY = \&RC_FOUND;  # 302 was renamed in the standard
  166. push(@EXPORT, "RC_MOVED_TEMPORARILY");
  167.  
  168.  
  169. =head1 FUNCTIONS
  170.  
  171. The following additional functions are provided.  Most of them are
  172. exported by default.
  173.  
  174. =over 4
  175.  
  176. =item status_message($code)
  177.  
  178. The status_message() function will translate status codes to human
  179. readable strings. The string is the same as found in the constant
  180. names above.  If the $code is unknown, then C<undef> is returned.
  181.  
  182. =cut
  183.  
  184. sub status_message ($)
  185. {
  186.     $StatusCode{$_[0]};
  187. }
  188.  
  189. =item is_info($code)
  190.  
  191. Return TRUE if C<$code> is an I<Informational> status code.  This
  192. class of status code indicates a provisional response which can't have
  193. any content.
  194.  
  195. =item is_success($code)
  196.  
  197. Return TRUE if C<$code> is a I<Successful> status code.
  198.  
  199. =item is_redirect($code)
  200.  
  201. Return TRUE if C<$code> is a I<Redirection> status code. This class of
  202. status code indicates that further action needs to be taken by the
  203. user agent in order to fulfill the request.
  204.  
  205. =item is_error($code)
  206.  
  207. Return TRUE if C<$code> is an I<Error> status code.  The function
  208. return TRUE for both client error or a server error status codes.
  209.  
  210. =item is_client_error($code)
  211.  
  212. Return TRUE if C<$code> is an I<Client Error> status code. This class
  213. of status code is intended for cases in which the client seems to have
  214. erred.
  215.  
  216. This function is B<not> exported by default.
  217.  
  218. =item is_server_error($code)
  219.  
  220. Return TRUE if C<$code> is an I<Server Error> status code. This class
  221. of status codes is intended for cases in which the server is aware
  222. that it has erred or is incapable of performing the request.
  223.  
  224. This function is B<not> exported by default.
  225.  
  226. =back
  227.  
  228. =cut
  229.  
  230. sub is_info         ($) { $_[0] >= 100 && $_[0] < 200; }
  231. sub is_success      ($) { $_[0] >= 200 && $_[0] < 300; }
  232. sub is_redirect     ($) { $_[0] >= 300 && $_[0] < 400; }
  233. sub is_error        ($) { $_[0] >= 400 && $_[0] < 600; }
  234. sub is_client_error ($) { $_[0] >= 400 && $_[0] < 500; }
  235. sub is_server_error ($) { $_[0] >= 500 && $_[0] < 600; }
  236.  
  237. 1;
  238.  
  239. =head1 BUGS
  240.  
  241. Wished @EXPORT_OK had been used instead of @EXPORT in the beginning.
  242. Now too much is exported by default.
  243.  
  244. =cut
  245.